package com.example.a_java.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.a_java.common.Result;
import com.example.a_java.entity.SysDict;
import com.example.a_java.entity.SysDictData;
import com.example.a_java.service.SysDictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 字典类型管理控制器
 */
@Tag(name = "字典管理", description = "字典管理相关接口")
@RestController
@RequestMapping("/system/dict")
@SecurityRequirement(name = "bearer-key")
public class SysDictController {

    @Autowired
    private SysDictService dictService;

    /**
     * 获取字典类型列表
     */
    @Operation(summary = "获取字典类型列表")
    @GetMapping("/type/list")
    @PreAuthorize("hasAuthority('system:dict:list')")
    public Result<Page<SysDict>> typeList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "字典名称") @RequestParam(required = false) String dictName,
            @Parameter(description = "字典类型") @RequestParam(required = false) String dictType,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        
        Page<SysDict> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysDict> wrapper = new LambdaQueryWrapper<>();
        
        wrapper.like(StringUtils.hasText(dictName), SysDict::getDictName, dictName)
                .like(StringUtils.hasText(dictType), SysDict::getDictType, dictType)
                .eq(status != null, SysDict::getStatus, status)
                .orderByAsc(SysDict::getDictId);
        
        dictService.page(page, wrapper);
        return Result.success(page);
    }

    /**
     * 获取字典类型详情
     */
    @Operation(summary = "获取字典类型详情")
    @GetMapping("/type/{dictId}")
    @PreAuthorize("hasAuthority('system:dict:query')")
    public Result<SysDict> getTypeInfo(@PathVariable Long dictId) {
        return Result.success(dictService.getById(dictId));
    }

    /**
     * 新增字典类型
     */
    @Operation(summary = "新增字典类型")
    @PostMapping("/type")
    @PreAuthorize("hasAuthority('system:dict:add')")
    public Result<Boolean> addType(@RequestBody @Valid SysDict dict) {
        return Result.success(dictService.save(dict));
    }

    /**
     * 修改字典类型
     */
    @Operation(summary = "修改字典类型")
    @PutMapping("/type")
    @PreAuthorize("hasAuthority('system:dict:edit')")
    public Result<Boolean> updateType(@RequestBody @Valid SysDict dict) {
        return Result.success(dictService.updateById(dict));
    }

    /**
     * 删除字典类型
     */
    @Operation(summary = "删除字典类型")
    @DeleteMapping("/type/{dictId}")
    @PreAuthorize("hasAuthority('system:dict:delete')")
    public Result<Boolean> deleteType(@PathVariable Long dictId) {
        return Result.success(dictService.removeById(dictId));
    }

    /**
     * 获取字典数据列表
     */
    @Operation(summary = "获取字典数据列表")
    @GetMapping("/data/list")
    @PreAuthorize("hasAuthority('system:dict:list')")
    public Result<Page<SysDictData>> dataList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "字典类型") @RequestParam String dictType,
            @Parameter(description = "字典标签") @RequestParam(required = false) String dictLabel,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        
        Page<SysDictData> page = dictService.getDictDataPage(pageNum, pageSize, dictType, dictLabel, status);
        return Result.success(page);
    }

    /**
     * 根据字典类型查询字典数据
     */
    @Operation(summary = "根据字典类型查询字典数据")
    @GetMapping("/data/type/{dictType}")
    public Result<List<SysDictData>> getDictDataByType(@PathVariable String dictType) {
        List<SysDictData> data = dictService.getDictDataByType(dictType);
        return Result.success(data);
    }

    /**
     * 获取字典数据详情
     */
    @Operation(summary = "获取字典数据详情")
    @GetMapping("/data/{dictCode}")
    @PreAuthorize("hasAuthority('system:dict:query')")
    public Result<SysDictData> getDataInfo(@PathVariable Long dictCode) {
        SysDictData dictData = dictService.getDictDataById(dictCode);
        return Result.success(dictData);
    }

    /**
     * 新增字典数据
     */
    @Operation(summary = "新增字典数据")
    @PostMapping("/data")
    @PreAuthorize("hasAuthority('system:dict:add')")
    public Result<Boolean> addData(@RequestBody @Valid SysDictData dictData) {
        return Result.success(dictService.saveDictData(dictData));
    }

    /**
     * 修改字典数据
     */
    @Operation(summary = "修改字典数据")
    @PutMapping("/data")
    @PreAuthorize("hasAuthority('system:dict:edit')")
    public Result<Boolean> updateData(@RequestBody @Valid SysDictData dictData) {
        return Result.success(dictService.updateDictData(dictData));
    }

    /**
     * 删除字典数据
     */
    @Operation(summary = "删除字典数据")
    @DeleteMapping("/data/{dictCode}")
    @PreAuthorize("hasAuthority('system:dict:delete')")
    public Result<Boolean> deleteData(@PathVariable Long dictCode) {
        return Result.success(dictService.removeDictData(dictCode));
    }
} 
